package com.buba.springboot.controller;
import com.buba.springboot.pojo.user;
import com.buba.springboot.pojo.vo.pageVo;
import com.buba.springboot.service.userService;
import com.buba.springboot.utils.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    private userService UserService;

    @PostMapping ("findall")
    public PageInfo<user> findall(@RequestBody pageVo limit) {
        PageInfo<user> page = UserService.findall(limit);
        System.out.println(page);
        return page;
    }

    @DeleteMapping("/{id}")
    public R deleUser(@PathVariable Integer id) {
        UserService.deleteUser(id);
        if (id != null) {
            return R.ok().data("删除成功", id);
        } else {
            return R.error();
        }
    }



    //批量删除
    @PostMapping("/dels")
    public Map<String,Object> dels(@RequestBody List<Integer> id) {
        log.info("idss"+id);
           UserService.dels(id);
           Map<String,Object> map=new HashMap<>();
           return map;
    }

    @PostMapping("updateUser")
    public R updateUser(@RequestBody user form) {
        UserService.updateUser(form);
        if (form != null) {
            return R.ok().data("修改成功", form);
        } else {
            return R.error();
        }
    }
    //导出
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<user> list=UserService.list();
        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
//        writer.addHeaderAlias("username", "用户名");
//        writer.addHeaderAlias("usercode", "职位");
//        writer.addHeaderAlias("password", "密码");
//        writer.addHeaderAlias("address", "地址");
//        writer.addHeaderAlias("email", "邮箱");
//        writer.addHeaderAlias("address", "地址");
//        writer.addHeaderAlias("createTime", "创建时间");
//        writer.addHeaderAlias("modifydate", "威威企鹅");
//        writer.addHeaderAlias("delete_flag", "大撒大撒");


        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        //指定编码格式并创建文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        //让浏览器区分需要处理的数据类型，对应处理相同的数据             指定文件名
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

}
